import { Image, UploadToken } from "../type/entity";
import { httpImg, http } from "../util/http";
export interface RemoveResult{
  success:boolean
}
export interface UploadResult{
  err: number,
  url?: string
  msg?: string
  id?: string
}

export function getUploadToken(){
  return http.post<UploadToken>('/getUpLoadPass')
}

export function uploadImageFile(file:File, uploadToken:UploadToken){
  return httpImg.post<UploadResult>('', {file, ...uploadToken})
}

export function uploadImageBase64(dataUri:string, uploadToken:UploadToken){
  return httpImg.post<UploadResult>('', {b64_data: dataUri, ...uploadToken})
}

export function uploadImageUrl(url:string, uploadToken:UploadToken){
  return httpImg.post<UploadResult>('', {src: url, ...uploadToken})
}

// 保存图片信息到后端
export function saveImage(url:string, yunId:string, categoryId:number){
  return http.post<Image>('/saveImg', {url, yunId, categoryId})
}

export function getImages(){
  return http.post<Image[]>('/getImgsByAccount')
}

export function removeImgs(ids:number[]){
  return http.post<RemoveResult>('/removeImgs', {ids})
}

export function removeImg(id:number){
  return removeImgs([id])
}

export function updateImg(imgId:number, categoryId:number){
  return http.post<{success:boolean}>('/updateImgCategoryId', {imgId, categoryId})
}